package com.sc.nft.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.meta.component.utils.security.EncryptHandler;
import com.sc.nft.admin.request.DecryptTextRequest;
import com.sc.nft.admin.request.EncryptTextRequest;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "API网关安全管理")
@RequestMapping("/admin/gateway/security")
public class GatewaySecurityController {

    @SysMemberLogin
    @PostMapping(value = "/versions")
    @ApiOperation(value = "加密算法版本", notes = "可选择的加密算法版本", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<List<String>> versions() {
        return Result.success(Arrays.asList("V1", "V2"));
    }

    //    @SysMemberLogin
    @PostMapping(value = "/encrypt", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "加密API", notes = "对传入文本进行加密", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<String> encrypt(@Valid @RequestBody EncryptTextRequest request) {
        if (StrUtil.isBlank(request.getVersion()) || !versions().getData().contains(request.getVersion())) {
            return Result.fail("不支持的加密算法版本 " + request.getVersion());
        }

        try {
            EncryptHandler handler = EncryptHandler.of(request.getVersion());
            String ciphertext = handler.encryptAndBase64Encode(handler.key(), request.getText());
            return Result.success(ciphertext);
        } catch (Exception e) {
            log.error("加密出错 text: {}", request.getText(), e);
            return Result.fail(e.getMessage());
        }
    }

    //    @SysMemberLogin
    @PostMapping(value = "/decrypt", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "解密API", notes = "对传入文本进行解密", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<String> decrypt(@Valid @RequestBody DecryptTextRequest request) {
        if (StrUtil.isBlank(request.getVersion()) || !versions().getData().contains(request.getVersion())) {
            return Result.fail("不支持的加密算法版本 " + request.getVersion());
        }

        try {
            EncryptHandler handler = EncryptHandler.of(request.getVersion());
            String text = handler.decryptAndBase64Decode(handler.key(), request.getCiphertext());
            return Result.success(text);
        } catch (Exception e) {
            log.error("解密出错 text: {}", request.getCiphertext(), e);
            return Result.fail(e.getMessage());
        }
    }

}
